//
//  CZWriteNovelViewController.m
//  nsj_iOS
//
//  Created by apple on 2017/1/24.
//  Copyright © 2017年 GCZ. All rights reserved.
//

#import "CZWriteNovelViewController.h"

@interface CZWriteNovelViewController ()
@property (weak, nonatomic) IBOutlet UITextView *novelContentTV;
@property (weak, nonatomic) IBOutlet UITextField *titleTF;

@end

@implementation CZWriteNovelViewController {
    BOOL _success;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"上传故事";
    _success = NO;
    UIButton *rightBarBtn = [UIButton buttonWithType:(UIButtonTypeSystem)];
    rightBarBtn.frame = CGRectMake(0, 0, 54, 25);
    [rightBarBtn addTarget:self action:@selector(saveNovel) forControlEvents:(UIControlEventTouchUpInside)];
    rightBarBtn.backgroundColor = mRGBColor(214, 221, 220);
    [rightBarBtn setTitle:@"保存" forState:(UIControlStateNormal)];
    rightBarBtn.layer.cornerRadius = 2;
    rightBarBtn.clipsToBounds = YES;
    rightBarBtn.tintColor = ThemeColor;
    rightBarBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBarBtn];
    // Do any additional setup after loading the view from its nib.
    
    
}

- (void)saveNovel {
    NSString *urlString = [NSString stringWithFormat:@"%@/api/web/index.php/v1/doujin/upload-doujin",APP_HOST];
    if (self.titleTF.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入标题"];
        return;
    }
    if (self.novelContentTV.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"请输入正文"];
        return;
    }
    
    [GCZAFNetWorking POST:urlString params:@{@"token" : [[NSUserDefaults standardUserDefaults] objectForKey:User_Token], @"doujin_title" : self.titleTF.text, @"doujin_text" : self.novelContentTV.text, @"doujin_type" : @5} success:^(id response) {
        if ([response[@"code"] isEqualToString:@"200"]) {
            [SVProgressHUD showSuccessWithStatus:@"上传成功"];
            _success = YES;
        }
    } failure:^(NSURLSessionDataTask *operation, NSError *error) {
        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    if (self.callBack) {
        self.callBack(_success);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
